package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import vo.StockInfo;
import vo.UserInterest;

public class FileHandlerImpl implements FileHandler {

	/**
	 * This func gets stock information from the given interfaces path. If
	 * interfaces don't exit,or it has a illegal/malformed format, return NULL. The
	 * filepath can be a relative path or a absolute path
	 *
	 * @param filePath
	 * @return the Stock information array from the interfaces,or NULL
	 */
	@Override
	public StockInfo[] getStockInfoFromFile(String filePath) {
		// TODO: write your code here
		try {
			FileInputStream file_fi = new FileInputStream(filePath);
			InputStreamReader file_sr = new InputStreamReader(file_fi, "UTF-8");
			BufferedReader file = new BufferedReader(file_sr);

			boolean eof = false;

			final int MAXSI = 60;
			StockInfo[] siArray = new StockInfo[MAXSI];
			ArrayList<StockInfo> siArraylist = new ArrayList<StockInfo>();

			while (!eof) {
				String line = file.readLine();
				if (line == null) {
					eof = true;
				} else {
					StockInfo temp = new StockInfo();
					String[] data = line.split("\t");
					temp.setID(data[0]);
					temp.setTITLE(data[1]);
					temp.setAUTHOR(data[2]);
					temp.setDATE(data[3]);
					temp.setLASTUPDATE(data[4]);
					temp.setCONTENT(data[5]);
					temp.setANSWERAUTHOR(data[6]);
					temp.setANSWER(data[7]);
					siArraylist.add(temp);
				}
			}
			file.close();
			int i = -1;
			for (StockInfo temp : siArraylist) {
				if (i == -1) {
					i++;
					continue;
				}
				siArray[i] = temp;
				i++;
			}
			return siArray;
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
			return null;
		}
	}

	/**
	 * This func gets user interesting from the given interfaces path. If interfaces
	 * don't exit,or it has a illegal/malformed format, return NULL. The filepath
	 * can be a relative path or a absolute path
	 *
	 * @param filePath
	 * @return
	 */
	@Override
	public UserInterest[] getUserInterestFromFile(String filePath) {
		UserInterest[] userArray = new UserInterest[500];
		for (int i = 0; i < 500; i++) {
			userArray[i] = new UserInterest();
		}
		try {
			FileReader file = new FileReader(filePath);
			boolean eof = false;
			int input = 0;
			int column = 0, row = 0;
			while (!eof) {
				input = file.read();
				if (input == -1) {
					eof = true;
				} else if (input != 10) {
					if (row % 60 == 0 && row >= 60)
						column++;
					int[] userInterest = userArray[column].getInterestArray();
					userInterest[row % 60] = input - 48;
					// userArray[column].getInterestArray()[row%60] = input-48;
					row++;
				}
			}
			file.close();
		} catch (IOException e) {
			System.out.println("Error:" + e.getMessage());
		}
		return userArray;
	}

	/**
	 * This function need write matrix to files
	 *
	 * @param matrix
	 *            the matrix you calculate
	 */
	@Override
	public void setCloseMatrix2File(double[][] matrix) {
		// TODO: write your code here
		try {
			FileOutputStream file_fos = new FileOutputStream("C:\\Users\\胡伟\\Desktop\\Java-WorkSpace\\java-example-2\\src\\main\\resources\\SimilarityMatrix.txt");
			OutputStreamWriter file_osw = new OutputStreamWriter(file_fos, "UTF-8");
			BufferedWriter file = new BufferedWriter(file_osw);
			for (int i = 0; i < 60; i++) {
				for (int j = 0; j < 60; j++) {
					file.write(String.valueOf(matrix[i][j]));
					file.write('\t');
					;
				}
				file.write('\n');
			}
			file.close();
		} catch (IOException e) {
			System.out.println("Error:" + e.getMessage());
		}
	}

	/**
	 * This function need write recommend to files
	 *
	 * @param recommend
	 *            the recommend you calculate
	 */
	@Override
	public void setRecommend2File(double[][] recommend) {
		// TODO: write your code here
		try {
			FileOutputStream file_fos = new FileOutputStream("C:\\Users\\胡伟\\Desktop\\Java-WorkSpace\\java-example-2\\src\\main\\resources\\RecommendMatrix.txt");
			OutputStreamWriter file_osw = new OutputStreamWriter(file_fos, "UTF-8");
			BufferedWriter file = new BufferedWriter(file_osw);
			for (int i = 0; i < 500; i++) {
				for (int j = 0; j < 10; j++) {
					file.write(String.valueOf(recommend[i][j]));
					file.write('\t');
					;
				}
				file.write('\n');
			}
			file.close();
		} catch (IOException e) {
			System.out.println("Error:" + e.getMessage());
		}
	}
}
